package com.yhty.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yhty.service.OweManager;


/**
 * 启动监听器
 * 
 * @author roy 2011-3-4-下午01:18:18
 */
public class StartupListener extends ContextLoaderListener implements
		ServletContextListener {
	Logger logger = Logger.getLogger(StartupListener.class);

	/**
	 * 初始化上下文
	 */
	public void contextInitialized(ServletContextEvent event) {
		logger.info("初始化spring上下文");
		try {
			WebApplicationContextUtils.getRequiredWebApplicationContext(event
					.getServletContext());
		} catch (Exception e) {
			super.contextInitialized(event);
		}
		setupCache(event.getServletContext());
	}

	/**
	 * 启动时初始化缓存
	 * 
	 * @param context
	 */
	public void setupCache(ServletContext context) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);
		//初始化欠费信息
		OweManager oweManager = (OweManager)ctx.getBean("oweManager");
		oweManager.dealOwe();
	}

}
